﻿#建议保存编码为：bom头 + utf8

param
(
	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("upstream_ip")][string]$上游ip,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("upstream_port")][UInt16]$上游端口,

	[ValidateNotNullOrEmpty()]
	[Alias("upstream_weight")][byte]$上游权重 = 1,

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[ValidateScript({
			if ($_[0] -eq '/')
			{
				return $true
			}
			else
			{
				Write-Error '错误：必须以“/”开头'
			}
		})]
	[Alias("route")][string]$路由路径, # '/abc'  '/abc/*'   '/index.html'

	[parameter(Mandatory = $true)]
	[ValidateNotNullOrEmpty()]
	[Alias("name")][string]$路由名字,

	[string]$endpoints , #后面不要有/

	[string]$username = '',

	[string]$password = ''

)

if ( ($null -eq $endpoints) -or ($endpoints -eq '') )
{
	if ( ($null -eq $global:endpoints) -or ($global:endpoints -eq '') )
	{
		Write-Error '错误：endpoints值错误。返回码 21'
		exit 21
	}
	else
	{
		$endpoints = $global:endpoints
	}
}

$private:msg = @'
用法：
$t = ./lib/add上游.ps1 -上游ip '192.168.1.2' -上游端口 9082 -上游权重 2 -上游名字 '测试上游' -endpoints 'http://192.168.3.4:2379'
./lib/add路由.ps1 -上游id $t -路由路径 '/test1' -路由名字 '测试路由'  -endpoints 'http://192.168.3.4:2379'
'@
Write-Warning $private:msg

Pause

$t = & "${PSScriptRoot}/lib/add上游.ps1" -上游ip $上游ip -上游端口 $上游端口 -上游权重 $上游权重 -上游名字 $路由名字 -endpoints $endpoints
& "${PSScriptRoot}/lib/add路由.ps1" -上游id $t -路由路径 $路由路径 -路由名字 $路由名字 -endpoints $endpoints
